package com.studyroom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studyroom.entity.Announcement;
import com.studyroom.dto.SearchDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import java.time.LocalDateTime;

@Mapper
public interface AnnouncementMapper extends BaseMapper<Announcement> {
    
    @Select("<script>" +
            "SELECT a.*, " +
            "DATE_FORMAT(a.created_at, '%Y-%m-%d %H:%i:%s') as createdAt, " +
            "DATE_FORMAT(a.updated_at, '%Y-%m-%d %H:%i:%s') as updatedAt " +
            "FROM announcements a " +
            "WHERE 1=1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "  AND (a.title LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR a.content LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "<if test='status != null'>" +
            "  AND a.status = #{status} " +
            "</if>" +
            "<if test='startTime != null'>" +
            "  AND a.created_at &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "  AND a.created_at &lt;= #{endTime} " +
            "</if>" +
            "ORDER BY a.created_at DESC" +
            "</script>")
    @Results({
        @Result(column = "createdAt", property = "createdAt"),
        @Result(column = "updatedAt", property = "updatedAt")
    })
    IPage<Announcement> searchAnnouncements(
        Page<Announcement> page,
        @Param("keyword") String keyword,
        @Param("status") Integer status,
        @Param("startTime") LocalDateTime startTime,
        @Param("endTime") LocalDateTime endTime
    );
} 